<?php
	class LotteryAction extends BaseAction {
		
		public function get_lottery(){
			$uid = $_SESSION['member']['uid'];
			$map['uid']  = $uid;;
			$map['status']  = 1;
			$yes = D('lottery')->where($map)->find();
			if(!empty($yes)){
				$this->assign('yes',$yes);
			}
			$this->display();
		}
		public function get_code(){
			$uid = $_SESSION['member']['uid'];
			if($uid == ""){
				$this->error('获取不到您的uid!');
			}else{
				$where['uid']  = $uid;
				$where['phone']  = $_POST['phone'];
				$where['_logic'] = 'or';
				$map['_complex'] = $where;
				$map['status']  = 1;
				$yes = D('lottery')->where($map)->find();
				if(!empty($yes)){
					$this->error('该手机号已经领取过彩票了！');
					exit;
				}
				
				$m['status'] = 0;
				$lid = D('lottery')->where($m)->order('rand()')->limit(1)->getField('lid');
				$result = D('lottery')->where('lid ='.$lid)->save(array('phone'=>$_POST['phone'],'get_time'=>time(),'uid'=>$uid,'status'=>1));
				
				if($result){
						$this->success('领取成功',"__URL__/lottery_info");
					}else{
						$this->error('对不起，已经被领光了！');
				}
			}
		}

		public function lottery_info(){
			$uid = $_SESSION['member']['uid'];
			$m['status'] = 1;
			$list = D('lottery')->where($m)->order('rand()')->limit(10)->select();
				if(!empty($list)){
					foreach ($list as $k => &$v) {
						$v['phone'] = substr($v['phone'],0,4)."****".substr($v['phone'],8,3); 
					}
					$this->assign('list',$list);
				}
			$map['uid']  = $uid;;
			$map['status']  = 1;
			$yes = D('lottery')->where($map)->find();
			if(!empty($yes)){
				$this->assign('yes',$yes);
			}
			$this->display();
		}

		public function get_num(){
			$uid = $_SESSION['member']['uid'];
			if($uid == ""){
				$this->error('获取不到您的uid!');
			}else{
				$where['uid']  = $uid;
				$where['phone']  = $_POST['phone'];
				$where['_logic'] = 'or';
				$map['_complex'] = $where;
				$map['status']  = 1;
				$map['click']  = 1;
				$yes = D('lottery')->where($map)->find();
				if(!empty($yes)){
					$this->error('该手机号已经领取过彩票了！');
					exit;
				}
				$da['status'] = 1;
				$da['click'] = 0;
				$lo = D('lottery')->where($da)->select();
				$ti = strtotime("-600 seconds");
				foreach($lo as $k=>&$v){
					if($v['get_time']<$ti){
						D('lottery')->where('lid = '.$v['lid'])->save(array('status'=>0,'uid'=>NULL,'phone'=>NULL,'get_time'=>NULL));
					}
				}
				$m['status'] = 0;
				$lid = D('lottery')->where($m)->order('rand()')->limit(1)->getField('lid');
				D('lottery')->where('lid ='.$lid)->save(array('phone'=>$_POST['phone'],'get_time'=>time(),'uid'=>$uid,'status'=>1));
				$url = "http://www.rencailaile.com/weixin.php?s=/Lottery/send_lottery/uid/".$uid."/lid/".$lid;
				$url = $this->short_url($url);
				if($_POST['phone'] !=""){
						$msg = '恭喜您获得了一注彩票，请点击领取：'.$url.'。(请立即点击领取，有效期只有十分钟哦！)【人才来了】';
						$result = $this->SendMesg($msg,$_POST['phone']);
				}
				if($result){
						$this->success('领取成功');
					}else{
						$this->error('对不起，已经被领光了！');
				}
			}
			
		}

		public function send_lottery(){
			$uid = $_GET['uid'];
			$lid = $_GET['lid'];
			if(empty($lid)){
				$this->error('lid不能为空');
			}else{
				$lo = D('lottery')->where('lid ='.$lid)->find();
				if($lo['click'] == 0 && $lo['status'] == 1){
					$ti = strtotime("-600 seconds");
					if($lo['get_time']<$ti){
						D('lottery')->where('lid ='.$lid)->save(array('status'=>0,'uid'=>"",'phone'=>"",'get_time'=>""));
						$this->error('您已经超过十分钟未点击，请重新回到领奖页面领取彩票！');
					}else{
						if($lo['uid']!=$uid){
							$this->error('该链接中的uid与数据库中的uid不相等！');
						}else{
							D('lottery')->where('lid ='.$lid)->save(array('click'=>1,'get_ctime'=>time()));
							$this->assign('lo',$lo);

						}
					}
				}else{
					if($lo['uid']!=$uid){
							$this->error('该链接中的uid与数据库中的uid不相等！');
						}else{
							$this->assign('lo',$lo);

						}
				}
				
			}
			$this->display();
		}

		public function SendMesg($con,$num){
			require_once 'HttpClient.class.php';
			$client = new HttpClient ("www.lx198.com");
			if (! $client->post ( '/sdk/send', array (
					'accName' => "18621771369",
					'accPwd' => strtoupper ( md5 ( "dd200412" ) ),
					'bizId' => date ( 'YmdHis' ),
					'content' => mb_convert_encoding ( $con, 'UTF-8', 'UTF-8' ),
					'aimcodes' =>$num ,
					'dataType'=>"xml"
			) )) {
				return false;
			} else {
				$xmlStr = $client->getContent () ;
				$doc = new DOMDocument ();
				$doc->loadXML ( $xmlStr );
				$xml = simplexml_load_string($xmlStr);
				if($xml->replyCode == 1){
					return true;
				}else{
					return false;
				}
			}
		}

		public function short_url($url){
		$key = '1562966081'; 
   		$baseurl = 'http://api.t.sina.com.cn/short_url/shorten.json?source='.$key.'&url_long='.$url; 
   		$ch=curl_init(); 
	    curl_setopt($ch, CURLOPT_URL,$baseurl); 
	    curl_setopt($ch, CURLOPT_HEADER, 0); 
	    curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
	    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
	    $strRes=curl_exec($ch); 
	    curl_close($ch); 
	    $arrResponse=json_decode($strRes,true); 
	    if (isset($arrResponse->error) || !isset($arrResponse[0]['url_long']) || $arrResponse[0]['url_long'] == ''){
	    	return 0; 
	    } 
    	return $arrResponse[0]['url_short']; 
   
    	}
	}
?>